using System.Collections.Generic;
using Castle.ActiveRecord;

namespace Cooking.Core.Model
{
    [ActiveRecord]
    public class Recipe
    {
        [PrimaryKey(Generator = PrimaryKeyType.Increment)]
        public int Id { get; set; }

        [Property]
        public string Name { get; set; }


        [HasMany(typeof(RecipeIngredient), Cascade = ManyRelationCascadeEnum.All, Access = PropertyAccess.FieldLowercaseUnderscore)]
        public IList<RecipeIngredient> Ingredients
        {
            get
            {
                //return new List<RecipeIngredient>(_ingredients).AsReadOnly();
                return _ingredients;
            }
            set { _ingredients = value; }
        }

        [HasMany(typeof(PreparationStep), Cascade = ManyRelationCascadeEnum.All, Access = PropertyAccess.FieldLowercaseUnderscore, OrderBy = "SequenceNumber")]
        public IList<PreparationStep> Steps
        {
            get
            {
                //return new List<PreparationStep>(_steps).AsReadOnly();
                return _steps;
            }
            set
            {
                _steps = value;
            }
        }

        private IList<PreparationStep> _steps = new List<PreparationStep>();
        private IList<RecipeIngredient> _ingredients = new List<RecipeIngredient>();

        public void AddIngredient(RecipeIngredient ingredient)
        {
            ingredient.BelongsTo = this;
            _ingredients.Add(ingredient);
        }

        public void AddIngredient(Ingredient ingredient, string unitOfMeasure, decimal quantity, string ingredientPrep)
        {
            RecipeIngredient recipeIngredient = new RecipeIngredient
                                                    {
                                                        BelongsTo = this,
                                                        Ingredient = ingredient,
                                                        IngredientPrep = ingredientPrep,
                                                        Quantity = quantity,
                                                        UnitOfMeasure = unitOfMeasure
                                                    };

            _ingredients.Add(recipeIngredient);
        }

        public void RemoveIngredient(RecipeIngredient ingredient)
        {
            _ingredients.Remove(ingredient);
        }

        public void AddStep(PreparationStep step)
        {
            step.BelongsTo = this;
            _steps.Add(step);
        }

        public void RemoveStep(PreparationStep step)
        {
            _steps.Remove(step);
        }
            
    }
}